#ifndef VETORES
#define VETORES

#include <GL/glut.h>
#include <math.h>
#include <stdio.h> 

#include "sistema.h"
#include "fmmt.h"
#include "menuvet.h"
#include "miscfunc.h"

extern sistema ms;
extern fmmt estator, rotor, resul;
extern grafico grafp, grafq, grafs, grafe, grafpt;
extern grafico grafwr, grafi, grafcosfi, grafdelta;
extern void controleRedisplay();

int janvetores;
bool firstvetores=true;
bool showvetores=false;
bool deltag=false, cosfig=false, ig=false, wrg=false;
bool ptg=false, eg=false, sg=false, qg=false, pg=false, none=true;
extern bool ok, step;

void vetoresDisplay(void)
{
	glClearColor (PRETO,1.0);
	glClear (GL_COLOR_BUFFER_BIT);

	ms.drawVetores(0,100,(TAM_J1_X/2) / (1.7*VBASE));

	glLineWidth(1);
	ms.drawpotencias(-220,-140,1);

	if (deltag)
	{
		glLineWidth(1);
		glColor3f(COR_GRAF_AXIS);
		grafdelta.drawaxis(-120,-230,360,200);
		glColor3f(COR_GRAF_PT);
		grafdelta.drawpoints(-120,-230,360,200);
		glColor3f(COR_GRAF_LEG);
		grafdelta.drawnames(-120,-230,360,200);
	}
	if (cosfig)
	{
		glLineWidth(1);
		glColor3f(COR_GRAF_AXIS);
		grafcosfi.drawaxis(-120,-230,360,200);
		glColor3f(COR_GRAF_PT);
		grafcosfi.drawpoints(-120,-230,360,200);
		glColor3f(COR_GRAF_LEG);
		grafcosfi.drawnames(-120,-230,360,200);
	}
	if (ig)
	{
		glLineWidth(1);
		glColor3f(COR_GRAF_AXIS);
		grafi.drawaxis(-120,-230,360,200);
		glColor3f(COR_GRAF_PT);
		grafi.drawpoints(-120,-230,360,200);
		glColor3f(COR_GRAF_LEG);
		grafi.drawnames(-120,-230,360,200);
	}
	if (wrg)
	{
		glLineWidth(1);
		glColor3f(COR_GRAF_AXIS);
		grafwr.drawaxis(-120,-230,360,200);
		glColor3f(COR_GRAF_PT);
		grafwr.drawpoints(-120,-230,360,200);
		glColor3f(COR_GRAF_LEG);
		grafwr.drawnames(-120,-230,360,200);
	}
	if (ptg)
	{
		glLineWidth(1);
		glColor3f(COR_GRAF_AXIS);
		grafpt.drawaxis(-120,-230,360,200);
		glColor3f(COR_GRAF_PT);
		grafpt.drawpoints(-120,-230,360,200);
		glColor3f(COR_GRAF_LEG);
		grafpt.drawnames(-120,-230,360,200);
	}
	if (eg)
	{
		glLineWidth(1);
		glColor3f(COR_GRAF_AXIS);
		grafe.drawaxis(-120,-230,360,200);
		glColor3f(COR_GRAF_PT);
		grafe.drawpoints(-120,-230,360,200);
		glColor3f(COR_GRAF_LEG);
		grafe.drawnames(-120,-230,360,200);
	}
	if (sg)
	{
		glLineWidth(1);
		glColor3f(COR_GRAF_AXIS);
		grafs.drawaxis(-120,-230,360,200);
		glColor3f(COR_GRAF_PT);
		grafs.drawpoints(-120,-230,360,200);
		glColor3f(COR_GRAF_LEG);
		grafs.drawnames(-120,-230,360,200);
	}
	if (pg)
	{
		glLineWidth(1);
		glColor3f(COR_GRAF_AXIS);
		grafp.drawaxis(-120,-230,360,200);
		glColor3f(COR_GRAF_PT);
		grafp.drawpoints(-120,-230,360,200);
		glColor3f(COR_GRAF_LEG);
		grafp.drawnames(-120,-230,360,200);
	}
	if (qg)
	{
		glLineWidth(1);
		glColor3f(COR_GRAF_AXIS);
		grafq.drawaxis(-120,-230,360,200);
		glColor3f(COR_GRAF_PT);
		grafq.drawpoints(-120,-230,360,200);
		glColor3f(COR_GRAF_LEG);
		grafq.drawnames(-120,-230,360,200);
	}

	glutSwapBuffers();
}

void vetoresReshape (int w, int h)
{
	glutReshapeWindow(TAM_J1_X, TAM_J1_Y);
	glViewport (0, 0, (GLsizei) w, (GLsizei) h);
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
	glOrtho (0.0, (GLdouble) TAM_J1_X, 0.0, (GLdouble) TAM_J1_Y, -1, 1);
	glTranslatef((GLdouble) TAM_J1_X/2, (GLdouble) TAM_J1_Y/2, 0);
}

void vetoresKeyboard(unsigned char key, int x, int y)
{
	switch (key) 
	{
	case 9:
		glutFullScreen();
		break;
	case 'r':
		vetoresReshape(TAM_J1_X,TAM_J1_Y);
		break;
	case ' ':
		ok=!ok;
		break;
	case 13:
		step=true;
		break;
	case '0':
		deltag=cosfig=ig=wrg=ptg=eg=sg=pg=qg=false;
		glutPostRedisplay();
		break;
	case '1':
		deltag=!deltag;
		cosfig=ig=wrg=ptg=eg=sg=pg=qg=false;
		glutPostRedisplay();
		break;
	case '2':
		cosfig=!cosfig;
		deltag=ig=wrg=ptg=eg=sg=pg=qg=false;
		glutPostRedisplay();
		break;
	case '3':
		ig=!ig;
		deltag=cosfig=wrg=ptg=eg=sg=pg=qg=false;
		glutPostRedisplay();
		break;
	case '4':
		wrg=!wrg;
		deltag=cosfig=ig=ptg=eg=sg=pg=qg=false;
		glutPostRedisplay();
		break;
	case '5':
		ptg=!ptg;
		deltag=cosfig=ig=wrg=eg=sg=pg=qg=false;
		glutPostRedisplay();
		break;
	case '6':
		eg=!eg;
		deltag=cosfig=ig=wrg=ptg=sg=pg=qg=false;
		glutPostRedisplay();
		break;
	case '7':
		sg=!sg;
		deltag=cosfig=ig=wrg=ptg=eg=pg=qg=false;
		glutPostRedisplay();
		break;
	case '8':
		pg=!pg;
		deltag=cosfig=ig=wrg=ptg=eg=sg=qg=false;
		glutPostRedisplay();
		break;
	case '9':
		qg=!qg;
		deltag=cosfig=ig=wrg=ptg=eg=sg=pg=false;
		glutPostRedisplay();
		break;
	case 'p':
		ms.report();
		break;
	case 'e':
		ms.setIEXC(ms.iexc+2.5);
		controleRedisplay();
		break;
	case 'd':
		ms.setIEXC(ms.iexc-2.5);
		controleRedisplay();
		break;
	case 'q':
		ms.setPT(ms.pt+100000);
		controleRedisplay();
		break;
	case 'a':
		ms.setPT(ms.pt-100000);
		controleRedisplay();
		break;
	case 't':
		ms.go(20000);
		controleRedisplay();
		break;
	case 27:
		glutSetWindow(janvetores);
		glutHideWindow();
		showvetores=false;
		break;
	}
}

int vetoresCreate(void)
{
    glutInitDisplayMode (GLUT_RGBA | GLUT_DOUBLE);
    glutInitWindowSize (500, 500);
    glutInitWindowPosition (0, 0);
    janvetores=glutCreateWindow (MSG142);

    glClearColor (0.0, 0.0, 0.0, 0.0);
    glShadeModel (GL_FLAT);

	ms.vetorV=true;
	ms.vetorE=true;
	ms.vetorJXSI=true;
	ms.vetorI=true;
	ms.vetorFE=false;
	ms.vetorFR=false;
	ms.vetorFRES=false;
	ms.legendas=true;
	ms.barrap=true;
	ms.barraq=true;
	ms.barras=true;
	ms.escala=true;

    glShadeModel (GL_SMOOTH);

	glutDisplayFunc(vetoresDisplay);
    glutReshapeFunc(vetoresReshape);
    glutKeyboardFunc(vetoresKeyboard);
	vetoresCreateMenu();
    return 0;
}

#define EXCENGL 0
#define EXCPORT 1
#define MATLAB 2

void savegraf(int fmt, FILE *arq)
{
	int j=0,cont=0;
	unsigned int k=0;
	point a,b,c,d,e,f,g,h,i;

	switch(fmt)
	{
	case 0:
	case 1: //opcao falecida... implentar algo aqui algum dia...
//		fprintf(arq, MSG143);
//		fprintf(arq, MSG144);
		fprintf(arq, MSG145);
		do
		{
			a=grafp.export(j);
			b=grafq.export(j);
			c=grafs.export(j);
			d=grafe.export(j);
			e=grafpt.export(j);
			f=grafwr.export(j);
			g=grafi.export(j);
			h=grafcosfi.export(j);
			i=grafdelta.export(j);
			if(a.x>0)
				fprintf(arq,"%10.5lf,%10.5lf,%10.5lf,%10.5lf,%10.5lf,%10.5lf,%10.5lf,%10.5lf,%10.5lf,%10.5lf\n",a.x, a.y*SBASE/1000, b.y*SBASE/1000, c.y*SBASE/1000, d.y, e.y*SBASE/1000, f.y, g.y, h.y, i.y);
			j++;
		} while(a.x>0);
		break;
	case 2:
		fprintf(arq,MSG149);
		fprintf(arq,"clear;\n");
		fprintf(arq,MSG150);
		fprintf(arq,MSG151);
		fprintf(arq,MSG152);
		fprintf(arq,MSG153);
		fprintf(arq,MSG154);
		fprintf(arq,MSG155);
		fprintf(arq,MSG156);
		fprintf(arq,MSG157);
		fprintf(arq,MSG158);
		fprintf(arq,MSG159);
		fprintf(arq,MSG160);
		fprintf(arq,MSG161);

		cont=0;
		j=0;
		a.x=1;
		for(j=0;a.x>0;j++) a=grafp.export(j);
		cont=j-1;
		
		fprintf(arq,"t = [ ");
		for(j=0;j<cont;j++) 
		{
			a=grafp.export(j);
			fprintf(arq," %10.5lf\n",a.x);
		}
		fprintf(arq,"   ];\n\n");

		fprintf(arq,"p = [ ");
		for(j=0;j<cont;j++) 
		{
			a=grafp.export(j);
			fprintf(arq," %10.5lf\n",a.y);
		}
		fprintf(arq,"\n   ];\n\n");
			
		fprintf(arq,"q = [ ");
		for(j=0;j<cont;j++) 
		{
			b=grafq.export(j);
			fprintf(arq, " %10.5lf\n",b.y);
		}
		fprintf(arq,"\n   ];\n\n");

		fprintf(arq,"s = [ ");
		for(j=0;j<cont;j++) 
		{
			c=grafs.export(j);
			fprintf(arq," %10.5lf\n",c.y);
		}
		fprintf(arq,"\n   ];\n\n");
		
		fprintf(arq,"e = [ ");
		for(j=0;j<cont;j++) 
		{
			d=grafe.export(j);
			fprintf(arq," %10.5lf\n",d.y);
		}
		fprintf(arq,"\n   ];\n\n");
		
		fprintf(arq,"pt = [ ");
		for(j=0;j<cont;j++) 
		{
			e=grafpt.export(j);
			fprintf(arq," %10.5lf\n",e.y);
		}
		fprintf(arq,"\n   ];\n\n");

		fprintf(arq,"wr = [ ");
		for(j=0;j<cont;j++) 
		{
			f=grafwr.export(j);
			fprintf(arq," %10.5lf\n",f.y);
		}
		fprintf(arq,"\n   ];\n\n");

		fprintf(arq,"i = [ ");
		for(j=0;j<cont;j++) 
		{
			g=grafi.export(j);
			fprintf(arq," %10.5lf\n",g.y);
		}
		fprintf(arq,"\n   ];\n\n");

		fprintf(arq,"cosfi = [ ");
		for(j=0;j<cont;j++) 
		{
			h=grafcosfi.export(j);
			fprintf(arq," %10.5lf\n",h.y);
		}
		fprintf(arq,"\n   ];\n\n");

		fprintf(arq,"delta = [ ");
		for(j=0;j<cont;j++) 
		{
			i=grafdelta.export(j);
			fprintf(arq," %10.5lf\n",i.y);
		}
		fprintf(arq,"\n   ];\n\n");
		fprintf(arq,"%%Fim\n");
		break;
	}
}


void vetoresPrint_PS(FILE *arq)
{
	epsheader(arq,TAM_J1_X, TAM_J1_Y);
	epsfunc(arq);

	fprintf(arq, "gsave\n");
	fprintf(arq, "%lf pt %lf pt translate\n", (double)TAM_J1_X/2, (double)TAM_J1_Y/2);
	
	ms.plotVetores(arq, 0, 100.0, (TAM_J1_X/2) / (1.7*VBASE));
	ms.plotpotencias(arq, -220.0, -140.0, 1.0);

	fprintf(arq,"/%s findfont\n",ESC_FONT_NAME_PS);
	fprintf(arq,"%f scalefont setfont\n",(float)(ESC_FONT_SIZE_PS));

	fprintf(arq, "(%s)  100 -100 showcentertext\n","Maquina sincrona de polos lisos");
	fprintf(arq, "-5 -105 moveto 205 -105 lineto stroke\n");
	fprintf(arq, "(%s=%2.1lf %s)  0 -120 showlefttext\n", "VN", ms.v.abs()*sqrt(3)/1000,"KV");
	fprintf(arq, "-5 -125 moveto 205 -125 lineto stroke\n");
	fprintf(arq, "(%s=%5.1lf MVA)  0 -140 showlefttext\n", "SN", (double)SBASE/1000000);
	fprintf(arq, "-5 -145 moveto 205 -145 lineto stroke\n");
	fprintf(arq, "(%s=%5.1lf %s)  0 -160 showlefttext\n", "fN", 60.0, "Hz");
	fprintf(arq, "-5 -165 moveto 205 -165 lineto stroke\n");
	fprintf(arq, "(%s=%0.3lf %s)  0 -180 showlefttext\n", "cosphiN", COSFIN,"");
	fprintf(arq, "-5 -185 moveto 205 -185 lineto stroke\n");
	fprintf(arq, "(%s=%3.2lf %s)  0 -200 showlefttext\n", "Xs", ms.xs,"Ohm");
	fprintf(arq, "-5 -205 moveto 205 -205 lineto stroke\n");
	fprintf(arq, "(%s=%5d %s)  0 -220 showlefttext\n", "IexcN", (int)IEXC_NOM,"A");
	fprintf(arq, "-5 -225 moveto 205 -225 lineto stroke\n");

	fprintf(arq, "-5 -105 moveto -5 -225 lineto stroke\n");
	fprintf(arq, "205 -105 moveto 205 -225 lineto stroke\n");
	fprintf(arq, "95 -105 moveto 95 -225 lineto stroke\n");

	fprintf(arq, "(%s=%5.2lf %s)  100 -120 showlefttext\n", "Pturb.", ms.pt/1000000,"MW");
	fprintf(arq, "(%s=%5.1lf %s)  100 -140 showlefttext\n", "Iexc", ms.iexc,"A");
	fprintf(arq, "(%s=%5.2lf %s)  100 -160 showlefttext\n", "H", ms.H, "s");
	fprintf(arq, "(%s=%5.2lf %s)  100 -180 showlefttext\n", "J", ms.J, "kg.m^2");
	fprintf(arq, "(%s=%3d %s)  100 -200 showlefttext\n", "polos", 2,"");
	fprintf(arq, "(%s=%5d %s)  100 -220 showlefttext\n", "nrN", 3600,"RPM");

	fprintf(arq, "grestore\n");
	fprintf(arq, "1 setlinewidth\n");
	fprintf(arq, "%lf pt %lf pt %lf pt %lf pt quad\n\nstroke\n", 0.0, 0.0, 500.0, 500.0);
	
	epsfooter(arq);
}

#endif